var arr = [0,0,0,0];
var length = arr.length;
var i = 0;
var positionArr = ['positionFour','positionThree','positionTwo','positionOne'];
var numbers = [465234,90894448,18230545,120546];

var filterNumbers = numbers.map(function(item){
    return formatNum(item + '');
})

console.log('filterNumbers',filterNumbers)
filterNumbers.forEach(function(item,index){
    $('.planets .planet').eq(index).children('h3').text(item);
})


var demo = null;
var options = {
      useEasing: true, 
      useGrouping: true, 
      separator: ',', 
      decimal: '.', 
};

//将所有的数字金额每3位添加一个逗号，示例：888,888,888.8
function formatNum(str) {

    var newStr = "";

    var count = 0;



    if (str.indexOf(".") == -1) {

        for (var i = str.length - 1; i >= 0; i--) {

            if (count % 3 == 0 && count != 0) {

                newStr = str.charAt(i) + "," + newStr;

            } else {

                newStr = str.charAt(i) + newStr;

            }

            count++;

        }

        str = newStr; //自动补小数点后两位

        return str;

    }

    else {

        for (var i = str.indexOf(".") - 1; i >= 0; i--) {

            if (count % 3 == 0 && count != 0) {

                newStr = str.charAt(i) + "," + newStr;

            } else {

                newStr = str.charAt(i) + newStr; //逐个字符相接起来

            }

            count++;

        }

        str = newStr + (str + "00").substr((str + "00").indexOf("."), 3);

        return str;

    }

}

var timer = setInterval(function(){

    $('.planets .planet').removeClass('positionOne positionTwo positionThree positionFour active');
    for (var j=0;j<i + 1;j++)
    {
        if(arr[j] < 4) {
            arr[j] += 1;
        } 

        
    }

    for (var j=0;j<length;j++)
    {
        if(arr[j] !== 0) {
            $('.planets .planet').eq(j).addClass(positionArr[arr[j] - 1]);
        }else{
            break;
        }
        if(arr[j] === 3) {
            $('.planets .planet').eq(j).addClass('active')
            var preT = 0;
            var nowT = 0;
            if(j === 0) {              
                nowT = numbers[0];
            }else{
                preT = numbers[0];
                nowT = numbers[1];
            }

            demo = new CountUp('current_center_figure', preT, nowT, 0, 0.75, options);
            if (!demo.error) {
              demo.start();
            } else {
              console.error(demo.error);
            }              
        }
    }    

    i++;
    if(i === 4) {
        clearInterval(timer);
        rotate();

    }
    
},2000)

var rotate_arr = [3,2,1,0];
var rotate_timer = null;
function rotate(){
    rotate_timer = setInterval(function() {

        rotate_arr.forEach(function(item,index){
            item += 1;
            if(item > 3) {
                item = 0;
            }
            rotate_arr[index] = item;
        })

        $('.planet').removeClass('positionOne positionTwo positionThree positionFour active')
        $('.planets .planet').eq(0).addClass(positionArr[rotate_arr[0]]);
        $('.planets .planet').eq(1).addClass(positionArr[rotate_arr[1]]);
        $('.planets .planet').eq(2).addClass(positionArr[rotate_arr[2]]);
        $('.planets .planet').eq(3).addClass(positionArr[rotate_arr[3]]);  
        
        
        var activeIndex = rotate_arr.indexOf(2);
        $('.planets .planet').eq(activeIndex).addClass('active')
       //var text = $('.planets .planet').eq(activeIndex).children('h3').text();
       var text = numbers[activeIndex];
       var previous = activeIndex -1;
       if(previous < 0) {
           previous = 3;
       }else if (previous > 3){
           previous = 0;
       } 
       var previsousText = numbers[previous];
       //var previsousText = $('.planets .planet').eq(previous).children('h3').text(); 

       demo = new CountUp('current_center_figure', previsousText, text, 0, 0.75, options);
       if (!demo.error) {
         demo.start();
       } else {
         console.error(demo.error);
       }       

    }, 2000)
}


$('.animation_btn_box .animation_stop').click(function(){
    $(this).css('display','none');
    $('.animation_btn_box .animation_start').css('display','block');
    clearInterval(rotate_timer);

})

$('.animation_btn_box .animation_start').click(function(){
    $(this).css('display','none');
    $('.animation_btn_box .animation_stop').css('display','block'); 
    rotate();   
})
$('.animation_btn_box .fullScreen_stop').click(function(){
    $(this).css('display','none');
    $('.animation_btn_box .fullScreen_start').css('display','block');
    requestFullScreen(window.parent.document.documentElement);
})

$('.animation_btn_box .fullScreen_start').click(function(){
    $(this).css('display','none');
    $('.animation_btn_box .fullScreen_stop').css('display','block');  
    exitFull(window.parent.document); 
})